﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using QLHD.Models;
using System.Globalization;
using System.Web.Services;

namespace QLHD.WS
{
    public partial class UserService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod(enableSession: true)]
        public static object GetListUser(Search obj)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            List<User> lst = new List<User>();
            string keyword = obj.Keyword;
            int page = int.Parse(obj.page);
            int pagesize = int.Parse(obj.pageSize);
            int skip = int.Parse(obj.skip);
            int take = int.Parse(obj.take);
            int total = 0;

            //lstUser = db.Users.OrderByDescending(q => q.ID).Skip((page - 1) * pagesize).Take(pagesize).ToList();
            //total = db.Users.Count();

            string query = "SELECT * FROM  ( SELECT [User].*, ROW_NUMBER() OVER(ORDER BY ID DESC) AS RowNumber FROM [User]) temp WHERE temp.username !='admin' and temp.RowNumber BETWEEN @startRowNumber AND @endRowNumber";
            string condition = "";

            query = string.Format(query, condition);

            //check quyen xem hop dong thuoc phong nao
            SqlParameter startRowNumber = new SqlParameter("startRowNumber", (page - 1) * pagesize);
            SqlParameter endRowNumber = new SqlParameter("endRowNumber", page * pagesize);
            lst = db.ExecuteStoreQuery<User>(query, startRowNumber, endRowNumber).ToList<User>();
            total = lst.Count;

            DateTime now = DateTime.Now;
            int i = 1;
            return new { lst, total = total };
        }



        [WebMethod]
        public static void CreateUsers(IEnumerable<UserUI> obj)
        {
            QLHD2013Entities db = new QLHD2013Entities();

            User newUser = new User();

            foreach (UserUI item in obj)
            {
                if (db.Users.Where(q => q.UserName.ToLower() == item.Username.ToLower()).Count() > 0)
                {
                    continue;
                }
                newUser.UserName = item.Username;
                //ma hoa password
                //newUser.PassWord = item.Password;
                newUser.PassWord = Utilities.Utilities.Encrypt(item.Password);
                newUser.FullName = item.FullName;
                newUser.Email = item.Email;
                if (!string.IsNullOrEmpty(item.DateCreated))
                {
                    newUser.DateCreated = Utilities.Utilities.ConvertToDateTime(item.DateCreated);
                }
                else
                {
                    newUser.DateCreated = null;
                }

                db.AddToUsers(newUser);
            }
            db.SaveChanges();

        }

        [WebMethod]
        public static string UpdateUser(IEnumerable<UserUI> obj)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            string msgerror = "";
            List<string> duplicateusers =new List<string>();
            foreach (UserUI item in obj)
            {
                int id = int.Parse(item.ID);
                //da ton tai username tuong tu
                if (db.Users.Where(q => q.UserName.ToLower() == item.Username.ToLower() && q.ID != id).Count() > 0)
                {
                    duplicateusers.Add(item.Username);
                    continue;
                }
                User newUser = db.Users.Where(q => q.ID == id).FirstOrDefault();
                if (newUser != null)
                {
                    //newhd.MaHD = obj.MaHD;
                    newUser.UserName = item.Username;
                    if (newUser.PassWord != item.Password)//thay doi password khac
                    {
                        newUser.PassWord = Utilities.Utilities.Encrypt(item.Password);
                    }
                    newUser.FullName = item.FullName;
                    newUser.Email = item.Email;
                    if (!string.IsNullOrEmpty(item.DateCreated))
                    {
                        newUser.DateCreated = Utilities.Utilities.ConvertToDateTime(item.DateCreated);
                    }
                    else
                    {
                        newUser.DateCreated = null;
                    }
                }
            }
            db.SaveChanges();
            if (duplicateusers.Count() > 0)
            {
                if (duplicateusers.Count() == 1)
                    msgerror = "Tên đăng nhập " + duplicateusers[0] + " đã tồn tại.";
                else
                {
                    msgerror = "Các tên đăng nhập sau đã tồn tại: " + string.Join(",", duplicateusers) + ".";
                }
            }
            return msgerror;
        }

        [WebMethod]
        public static void DeleteUser(int ID)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            int id = ID;
            var user = db.Users.Where(q => q.ID == id).FirstOrDefault();
            if (user != null)
            {
                db.DeleteObject(user);
                //db.Users.DeleteObject();
                // db.SaveChanges();
            }
        }
    }
}